
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="zh">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>21.9. urllib.error --- Exception classes raised by urllib.request &#8212; Python 3.6.6rc1 文档</title>
    <link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript" src="../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/sidebar.js"></script>
    <link rel="search" type="application/opensearchdescription+xml"
          title="在 Python 3.6.6rc1 文档 中搜索"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="关于这些文档" href="../about.html" />
    <link rel="index" title="索引" href="../genindex.html" />
    <link rel="search" title="搜索" href="../search.html" />
    <link rel="copyright" title="Copyright" href="../copyright.html" />
    <link rel="next" title="21.10. urllib.robotparser --- Parser for robots.txt" href="urllib.robotparser.html" />
    <link rel="prev" title="21.8. urllib.parse --- Parse URLs into components" href="urllib.parse.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
    <link rel="canonical" href="https://docs.python.org/3/library/urllib.error.html" />
    
    <script type="text/javascript" src="../_static/copybutton.js"></script>
    <script type="text/javascript" src="../_static/switchers.js"></script>
    
    
 

  </head><body>  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">索引</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python 模块索引"
             >模块</a> |</li>
        <li class="right" >
          <a href="urllib.robotparser.html" title="21.10. urllib.robotparser --- Parser for robots.txt"
             accesskey="N">下一页</a> |</li>
        <li class="right" >
          <a href="urllib.parse.html" title="21.8. urllib.parse --- Parse URLs into components"
             accesskey="P">上一页</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &#187;</li>
        <li>
          <span class="language_switcher_placeholder">zh</span>
          <span class="version_switcher_placeholder">3.6.6rc1</span>
          <a href="../index.html">Documentation </a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="internet.html" accesskey="U">21. Internet Protocols and Support</a> &#187;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="Quick search" type="text" name="q" />
          <input type="submit" value="Go" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>    

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="module-urllib.error">
<span id="urllib-error-exception-classes-raised-by-urllib-request"></span><h1>21.9. <a class="reference internal" href="#module-urllib.error" title="urllib.error: Exception classes raised by urllib.request."><code class="xref py py-mod docutils literal notranslate"><span class="pre">urllib.error</span></code></a> --- Exception classes raised by urllib.request<a class="headerlink" href="#module-urllib.error" title="永久链接至标题">¶</a></h1>
<p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.6/Lib/urllib/error.py">Lib/urllib/error.py</a></p>
<hr class="docutils" />
<p>The <a class="reference internal" href="#module-urllib.error" title="urllib.error: Exception classes raised by urllib.request."><code class="xref py py-mod docutils literal notranslate"><span class="pre">urllib.error</span></code></a> module defines the exception classes for exceptions
raised by <a class="reference internal" href="urllib.request.html#module-urllib.request" title="urllib.request: Extensible library for opening URLs."><code class="xref py py-mod docutils literal notranslate"><span class="pre">urllib.request</span></code></a>.  The base exception class is <a class="reference internal" href="#urllib.error.URLError" title="urllib.error.URLError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">URLError</span></code></a>.</p>
<p>The following exceptions are raised by <a class="reference internal" href="#module-urllib.error" title="urllib.error: Exception classes raised by urllib.request."><code class="xref py py-mod docutils literal notranslate"><span class="pre">urllib.error</span></code></a> as appropriate:</p>
<dl class="exception">
<dt id="urllib.error.URLError">
<em class="property">exception </em><code class="descclassname">urllib.error.</code><code class="descname">URLError</code><a class="headerlink" href="#urllib.error.URLError" title="永久链接至目标">¶</a></dt>
<dd><p>The handlers raise this exception (or derived exceptions) when they run into
a problem.  It is a subclass of <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a>.</p>
<dl class="attribute">
<dt id="urllib.error.URLError.reason">
<code class="descname">reason</code><a class="headerlink" href="#urllib.error.URLError.reason" title="永久链接至目标">¶</a></dt>
<dd><p>The reason for this error.  It can be a message string or another
exception instance.</p>
</dd></dl>

<div class="versionchanged">
<p><span class="versionmodified">在 3.3 版更改: </span><a class="reference internal" href="#urllib.error.URLError" title="urllib.error.URLError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">URLError</span></code></a> has been made a subclass of <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> instead
of <a class="reference internal" href="exceptions.html#IOError" title="IOError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">IOError</span></code></a>.</p>
</div>
</dd></dl>

<dl class="exception">
<dt id="urllib.error.HTTPError">
<em class="property">exception </em><code class="descclassname">urllib.error.</code><code class="descname">HTTPError</code><a class="headerlink" href="#urllib.error.HTTPError" title="永久链接至目标">¶</a></dt>
<dd><p>Though being an exception (a subclass of <a class="reference internal" href="#urllib.error.URLError" title="urllib.error.URLError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">URLError</span></code></a>), an
<a class="reference internal" href="#urllib.error.HTTPError" title="urllib.error.HTTPError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">HTTPError</span></code></a> can also function as a non-exceptional file-like return
value (the same thing that <a class="reference internal" href="urllib.request.html#urllib.request.urlopen" title="urllib.request.urlopen"><code class="xref py py-func docutils literal notranslate"><span class="pre">urlopen()</span></code></a> returns).  This
is useful when handling exotic HTTP errors, such as requests for
authentication.</p>
<dl class="attribute">
<dt id="urllib.error.HTTPError.code">
<code class="descname">code</code><a class="headerlink" href="#urllib.error.HTTPError.code" title="永久链接至目标">¶</a></dt>
<dd><p>An HTTP status code as defined in <span class="target" id="index-0"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc2616.html"><strong>RFC 2616</strong></a>.  This numeric value corresponds
to a value found in the dictionary of codes as found in
<a class="reference internal" href="http.server.html#http.server.BaseHTTPRequestHandler.responses" title="http.server.BaseHTTPRequestHandler.responses"><code class="xref py py-attr docutils literal notranslate"><span class="pre">http.server.BaseHTTPRequestHandler.responses</span></code></a>.</p>
</dd></dl>

<dl class="attribute">
<dt id="urllib.error.HTTPError.reason">
<code class="descname">reason</code><a class="headerlink" href="#urllib.error.HTTPError.reason" title="永久链接至目标">¶</a></dt>
<dd><p>This is usually a string explaining the reason for this error.</p>
</dd></dl>

<dl class="attribute">
<dt id="urllib.error.HTTPError.headers">
<code class="descname">headers</code><a class="headerlink" href="#urllib.error.HTTPError.headers" title="永久链接至目标">¶</a></dt>
<dd><p>The HTTP response headers for the HTTP request that caused the
<a class="reference internal" href="#urllib.error.HTTPError" title="urllib.error.HTTPError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">HTTPError</span></code></a>.</p>
<div class="versionadded">
<p><span class="versionmodified">3.4 新版功能.</span></p>
</div>
</dd></dl>

</dd></dl>

<dl class="exception">
<dt id="urllib.error.ContentTooShortError">
<em class="property">exception </em><code class="descclassname">urllib.error.</code><code class="descname">ContentTooShortError</code><span class="sig-paren">(</span><em>msg</em>, <em>content</em><span class="sig-paren">)</span><a class="headerlink" href="#urllib.error.ContentTooShortError" title="永久链接至目标">¶</a></dt>
<dd><p>This exception is raised when the <a class="reference internal" href="urllib.request.html#urllib.request.urlretrieve" title="urllib.request.urlretrieve"><code class="xref py py-func docutils literal notranslate"><span class="pre">urlretrieve()</span></code></a>
function detects that
the amount of the downloaded data is less than the expected amount (given by
the <em>Content-Length</em> header).  The <code class="xref py py-attr docutils literal notranslate"><span class="pre">content</span></code> attribute stores the
downloaded (and supposedly truncated) data.</p>
</dd></dl>

</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h4>上一个主题</h4>
  <p class="topless"><a href="urllib.parse.html"
                        title="上一章">21.8. <code class="docutils literal notranslate"><span class="pre">urllib.parse</span></code> --- Parse URLs into components</a></p>
  <h4>下一个主题</h4>
  <p class="topless"><a href="urllib.robotparser.html"
                        title="下一章">21.10. <code class="docutils literal notranslate"><span class="pre">urllib.robotparser</span></code> ---  Parser for robots.txt</a></p>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../bugs.html">Report a Bug</a></li>
      <li>
        <a href="https://github.com/python/cpython/blob/3.6/Doc/library/urllib.error.rst"
            rel="nofollow">Show Source
        </a>
      </li>
    </ul>
  </div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >索引</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python 模块索引"
             >模块</a> |</li>
        <li class="right" >
          <a href="urllib.robotparser.html" title="21.10. urllib.robotparser --- Parser for robots.txt"
             >下一页</a> |</li>
        <li class="right" >
          <a href="urllib.parse.html" title="21.8. urllib.parse --- Parse URLs into components"
             >上一页</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &#187;</li>
        <li>
          <span class="language_switcher_placeholder">zh</span>
          <span class="version_switcher_placeholder">3.6.6rc1</span>
          <a href="../index.html">Documentation </a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="internet.html" >21. Internet Protocols and Support</a> &#187;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="Quick search" type="text" name="q" />
          <input type="submit" value="Go" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>  
    <div class="footer">
    &copy; <a href="../copyright.html">Copyright</a> 2001-2018, Python Software Foundation.
    <br />
    The Python Software Foundation is a non-profit corporation.
    <a href="https://www.python.org/psf/donations/">Please donate.</a>
    <br />
    Last updated on 6月 24, 2018.
    <a href="../bugs.html">Found a bug</a>?
    <br />
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.7.2.
    </div>

  </body>
</html>